package com.user.basic.analyze.register;

import com.entity.analyze.RegisterType;
import org.springframework.stereotype.Service;

import java.lang.ref.Reference;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class WeekRegisterAnalyzeServiceImpl extends RegisterAnalyzeServiceImpl {
    public WeekRegisterAnalyzeServiceImpl(RegisterAnalyzeMapper registerAnalyzeMapper) {
        super(registerAnalyzeMapper);
    }

    @Override
    protected Map<String, Integer> transform(Reference<ConcurrentHashMap<String, List<Date>>> cache) {
        if (Objects.requireNonNull(cache.get()).size() > 0) {
            Map<String, Integer> countMap = new HashMap<>();
            cache.get().forEachValue(5000, u -> u.forEach(t -> {
                int quotients = (t.getDate() / 7);
                int surplus = t.getDate() % 7;
                if (surplus == 0) {
                    quotients -= 1;
                }
                countMap.compute((1900 + t.getYear()) + "-" + (t.getMonth() + 1) + "-" + (quotients + 1),
                        RegisterAnalyzeServiceImpl::apply);
            }));
            return countMap;
        }
        return null;
    }

    @Override
    protected void save(Map<Object, Integer> transform) {
        save(transform, RegisterType.Week);
    }
}
